package address

import (
	"context"

	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteUserAddressLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 删除UserAddress
func NewDeleteUserAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserAddressLogic {
	return &DeleteUserAddressLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteUserAddressLogic) DeleteUserAddress(
	req *types.IDsReq,
	headers *[]*types.Header,
) (resp *types.BaseMsgResp, err error) {
	if len(req.Ids) == 0 {
		return &types.BaseMsgResp{
			Msg: "删除成功",
		}, nil
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err = l.svcCtx.UserRpc.DeleteUserAddress(l.ctx, &userclient.RemoveIDsReq{
		Ids:       req.Ids,
		VisitorId: pointy.GetPointer(userId),
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Msg: "删除成功",
	}, nil
}
